package com.stms.service.impl;

import com.stms.dao.DepotRepository;
import com.stms.pojo.Depot;
import com.stms.service.DepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepotServiceImpl implements DepotService {
    @Autowired
    private DepotRepository depotRepository;

    @Override
    public boolean addDepot(Depot depot) {
        if(depotRepository.save(depot)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean editDepot(Depot depot) {
        if(depotRepository.save(depot)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void deleteBatch(String depotIds) {
        String[] depotStr = depotIds.split(",");
        List<Integer> list = new ArrayList<>();

        for(String str:depotStr){
            list.add(Integer.valueOf(str));
        }

        depotRepository.deleteBatch(list);
    }

    @Override
    public Page<Depot> fuzzyQuery(Integer pageNum, Integer pageSize, String direction, Depot depot) {
        if(pageNum == null||pageNum==0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize == null){
            pageSize=10;
        }
        if (direction == null){
            direction = new String("DESC");
        }

        Sort.Direction direc = direction.equals("DESC")?Sort.Direction.DESC:Sort.Direction.ASC;
        Pageable pageable = PageRequest.of(pageNum,pageSize,direc,"id");

        Page<Depot> page = depotRepository.findAll(new Specification<Depot>(){
            @Override
            public Predicate  toPredicate(Root<Depot> root, CriteriaQuery<?> query, CriteriaBuilder cb){
                List<Predicate> list =new ArrayList<>();
                if(depot == null){
                    return null;
                }
                if(depot.getId()!=null && !depot.getId().equals("")){
                    list.add(cb.equal(root.get("id"),depot.getId()));
                }
                if(depot.getAddress()!=null && !depot.getAddress().equals("")){
                    list.add(cb.like(root.get("address").as(String.class),"%"+depot.getAddress().trim()+"%"));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        },pageable);
        return page;
    }

    @Override
    public Page<Depot> findByPge(Integer pageNum, Integer pageSize) {
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        pageNum=pageNum-1;
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Depot> page = depotRepository.findAll(of);
        return page;
    }
}
